
package view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * @author dave
 *
 */

public class Kave86GUI {
	public JFrame entireFrame;
	ArrayList<JPanel> myPanels=new ArrayList<JPanel>();

	public Kave86GUI(){
		try { 
			UIManager.setLookAndFeel(
					UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		createAndShowGUI();
	}

	private void createAndShowGUI() {
		//Create and set up the window.
		entireFrame = new JFrame("kave86 simulator");
		entireFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		entireFrame.setVisible(true);
		makeAddPanels();
		entireFrame.pack();
		entireFrame.repaint();
	}

	private void makeAddPanels(){
		//Start us with a fresh Panel with some borders
		refreshContainer(entireFrame.getContentPane());
		
		
		//Create the "Bottom Panel". Add borders
		JPanel bottomPanel=new JPanel();
		refreshContainer(bottomPanel);
		//Add a register panel to the panel
		JPanel registerPanel=new RegistersPanel();
		bottomPanel.add(registerPanel, BorderLayout.LINE_START);
		//Add an execute button to the panel
		bottomPanel.add(makeExecuteButton(), BorderLayout.LINE_END);
		//Wrap the panel (for borders)
		JPanel wrapper=new JPanel();
		refreshContainer(wrapper);
		wrapper.add(bottomPanel, BorderLayout.CENTER);
		//Add the composite panel
		setPanel(wrapper, BorderLayout.PAGE_END);

		//Creating the textArea
		JTextArea textArea = new JTextArea( "" );
		textArea.setFont(new Font("Serif", Font.PLAIN, 16));
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		//Wrap it in a scrollpane in a Panel
		wrapper=new JPanel();
		JScrollPane areaScrollPane = new JScrollPane(textArea);
		areaScrollPane.setVerticalScrollBarPolicy(
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		areaScrollPane.setPreferredSize(new Dimension(600, 400));
		wrapper.add(areaScrollPane);
		//Add it to our main panel.
		setPanel(wrapper, BorderLayout.CENTER);
		
		//Create and wrap menu
		wrapper=new JPanel(new BorderLayout(5,5));
		JMenuBar menu = createMenu();
		wrapper.add(menu, BorderLayout.CENTER);
		//Add menu to top of frame
		setPanel(wrapper, BorderLayout.PAGE_START);
	}

	public JButton makeExecuteButton(){
		JButton button=new JButton("Execute");
		button.setPreferredSize(new Dimension(200,30));
		button.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						executeCode();
					}
				});
		button.setFont(new Font("Serif", Font.PLAIN, 25));
		return button;
	}

	protected void executeCode() {
		// TODO Grab from textfield, parse.

	}

	private void refreshContainer(Container c) {
		c.removeAll();
		c.setLayout(new BorderLayout(10, 10));
		c.add(makeNewPanel(), BorderLayout.LINE_START);
		c.add(makeNewPanel(), BorderLayout.LINE_END);
		c.add(makeNewPanel(), BorderLayout.PAGE_START);
		c.add(makeNewPanel(), BorderLayout.PAGE_END);
		c.add(makeNewPanel(), BorderLayout.CENTER);
	}

	private void setPanel(JPanel panel, String position){
		Container content=entireFrame.getContentPane();
		content.add(panel, position);
		content.validate();
		entireFrame.pack();
		entireFrame.repaint();
	}

	public void setPanel(int panelIndex, String position){
		if(myPanels.size()>panelIndex){
			JPanel tempPanel=myPanels.get(panelIndex);
			System.out.println(tempPanel);
			if(tempPanel!=null){
				setPanel(tempPanel, position);
			}
		}
	}


	/**
	 * Creates a new generic Panel with BorderLayout and borders (8,8)
	 * @return default panel with borderlayout and borders of 8.
	 */
	public static JPanel makeNewPanel(){
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout(5,5));
		return panel;
	}
	
	
	/**
	 * creates and populates the menu with File>Save, Variables>Add, 
	 * and Variables>Remove
	 */
	private JMenuBar createMenu() {
		JMenuBar jMenuBar1=new JMenuBar();
	
		jMenuBar1.add(createFileMenu());
		jMenuBar1.add(createEditMenu());
		jMenuBar1.add(createPreferencesMenu());
		

		return jMenuBar1;
	}
	/**
	 * Creates File Menu
	 */
	public JMenu createFileMenu()
	{
		JMenu fileMenu = new JMenu();
		//Create and populate "file"
		fileMenu.setText("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		fileMenu.getAccessibleContext().setAccessibleDescription(
		"The file menu");

		//a group of JMenuItems
		JMenuItem menuItem;
		menuItem = new JMenuItem("New Session",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		fileMenu.add(menuItem);
		
		menuItem = new JMenuItem("Save Session",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		fileMenu.add(menuItem);
		

		//-----------------

		menuItem = new JMenuItem();
		menuItem.setMnemonic(KeyEvent.VK_D);
		fileMenu.add(menuItem);
		
		//------------------

		
		menuItem = new JMenuItem("Close Session",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		fileMenu.add(menuItem);

		
		menuItem = new JMenuItem("Open Session",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		fileMenu.add(menuItem);
		
		return fileMenu;
	}
	/**
	 * Creates Variable Menu
	 */
	public JMenu createEditMenu()
	{
		JMenu editMenu = new JMenu();
		editMenu.setText("Edit");
		editMenu.setMnemonic(KeyEvent.VK_E);
		editMenu.getAccessibleContext().setAccessibleDescription(
		"The edit menu");
		
		JMenuItem menuItem;
		

		

		menuItem = new JMenuItem("Undo",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		editMenu.add(menuItem);
		

		
		menuItem = new JMenuItem("Redo",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		editMenu.add(menuItem);
		
		//-----------------

		menuItem = new JMenuItem();
		menuItem.setMnemonic(KeyEvent.VK_D);
		editMenu.add(menuItem);
		
		//------------------
		
		menuItem = new JMenuItem("Cut",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		editMenu.add(menuItem);

		menuItem = new JMenuItem("Copy",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_C, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		editMenu.add(menuItem);

		menuItem = new JMenuItem("Paste",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_P, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		editMenu.add(menuItem);

		//-----------------

		menuItem = new JMenuItem();
		menuItem.setMnemonic(KeyEvent.VK_D);
		editMenu.add(menuItem);
		
		//------------------
		
		menuItem = new JMenuItem("Select All",
				new ImageIcon(""));
		menuItem.setMnemonic(KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_A, ActionEvent.CTRL_MASK));
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){
						//Action
					}
				});
		editMenu.add(menuItem);		
		
		return editMenu;
	}
	/**
	 * Creates Information Menu - Variables and History
	 */
	public JMenu createPreferencesMenu()
	{ 
		JMenu preferencesMenu = new JMenu();
		preferencesMenu.setText("Preferences");
		preferencesMenu.setMnemonic(KeyEvent.VK_P);
		preferencesMenu.getAccessibleContext().setAccessibleDescription(
		"The preferences menu");
		
		JMenuItem menuItem;
		menuItem = new JMenuItem("Step-Wise Execution");
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){ 
						//Action
					}
				});
		preferencesMenu.add(menuItem);
		
		menuItem = new JMenuItem("Immediate Execution");
		menuItem.addActionListener(
				new ActionListener(){  
					public void actionPerformed (ActionEvent evt){ 
						//Action
					}
				});
		preferencesMenu.add(menuItem);

		//-----------------
		
		menuItem = new JMenuItem();
		menuItem.setMnemonic(KeyEvent.VK_D);
		preferencesMenu.add(menuItem);
		
		//------------------
		
		return preferencesMenu;
	}

}

